package com.anolesoft.yr.mbus.prod.pack.crtl;

import cn.hutool.core.bean.BeanUtil;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.prod.pack.dto.*;
import com.anolesoft.yr.mbus.prod.pack.entity.ProdPackage;
import com.anolesoft.yr.mbus.prod.pack.entity.ProdPackageService_;
import com.anolesoft.yr.mbus.prod.pack.entity.ProdPackageServiceValue;
import com.anolesoft.yr.mbus.prod.pack.dto.ProdPackageDeleteDTO;
import com.anolesoft.yr.mbus.prod.pack.dto.ProdPackageQueryDTO;
import com.anolesoft.yr.mbus.prod.pack.service.ProdPackageService;
import com.anolesoft.yr.mbus.prod.pack.vo.ProdPackageQueryVO;
import com.anolesoft.yr.mbus.prod.pack.vo.ProdPackageServiceQueryVO;
import com.anolesoft.yr.mbus.util.GlobalLoginInfo;
import com.anolesoft.yr.mbus.util.StrCP;
import com.anolesoft.yr.util.PageInfo;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@CrossOrigin
@RequestMapping(value = "/prodPackage")
public class ProdPackageController {

    @Autowired
    ProdPackageService prodPackageService;

    //新增产品套餐信息表
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult saveProdPackage(@RequestBody ProdPackage prodPackage) throws Exception {
        String prodPackageUid = StrCP.createObjuid32(UUID.randomUUID().toString());
        ProdPackageSaveDTO prodPackageSaveDTO = BeanUtil.copyProperties(prodPackage, ProdPackageSaveDTO.class);
        prodPackageSaveDTO.setObjuid(prodPackageUid);
        prodPackageSaveDTO.setFacCode("");
        prodPackageSaveDTO.setCcUserUid(GlobalLoginInfo.getGlobalLoginUser());
        prodPackageSaveDTO.setCcTime(GlobalLoginInfo.getGlobalLoginTime());
        prodPackageSaveDTO.setDelFlag(0);

        List prodPackageRelSaveDTOList = Lists.newArrayList();
        List prodPackageServiceSaveDTOList = Lists.newArrayList();

        List prodPackageServiceList = prodPackage.getProdPackageService_();
        for (int i = 0; i < prodPackageServiceList.size(); i++) {
            ProdPackageService_ prodPackageService_ = (ProdPackageService_) prodPackageServiceList.get(i);

            String prodPackageRellUid = StrCP.createObjuid32(UUID.randomUUID().toString());
            String prodPackageServiceUid = StrCP.createObjuid32(UUID.randomUUID().toString());
            ProdPackageRelSaveDTO prodPackageRelSaveDTO = new ProdPackageRelSaveDTO();
            prodPackageRelSaveDTO.setObjuid(prodPackageRellUid);
            prodPackageRelSaveDTO.setFacCode("");
            prodPackageRelSaveDTO.setProdPackageUid(prodPackageUid);
            prodPackageRelSaveDTO.setProdPackageServiceUid(prodPackageServiceUid);
            prodPackageRelSaveDTOList.add(prodPackageRelSaveDTO);

            ProdPackageServiceSaveDTO prodPackageServiceSaveDTO = BeanUtil.copyProperties(prodPackageService_, ProdPackageServiceSaveDTO.class);
            prodPackageServiceSaveDTO.setObjuid(prodPackageServiceUid);
            prodPackageServiceSaveDTO.setFacCode("");
            prodPackageServiceSaveDTO.setProdPackageUid(prodPackageUid);
            prodPackageServiceSaveDTOList.add(prodPackageServiceSaveDTO);

            List prodPackageServiceValueSaveDTOList = Lists.newArrayList();
            List prodPackageServiceValueList = prodPackageService_.getProdPackageServiceValue();
            for (int j = 0; j < prodPackageServiceValueList.size(); j++) {
                ProdPackageServiceValue prodPackageServiceValue = (ProdPackageServiceValue) prodPackageServiceValueList.get(j);

                String prodPackageServiceValueUid = StrCP.createObjuid32(UUID.randomUUID().toString());
                ProdPackageServiceValueSaveDTO prodPackageServiceValueSaveDTO = BeanUtil.copyProperties(prodPackageServiceValue, ProdPackageServiceValueSaveDTO.class);
                prodPackageServiceValueSaveDTO.setObjuid(prodPackageServiceValueUid);
                prodPackageServiceValueSaveDTO.setFacCode("");
                prodPackageServiceValueSaveDTO.setProdPackageUid(prodPackageUid);
                prodPackageServiceValueSaveDTO.setProdPackageServiceUid(prodPackageServiceUid);
                prodPackageServiceValueSaveDTOList.add(prodPackageServiceValueSaveDTO);
            }

            prodPackageServiceSaveDTO.setProdPackageServiceValueSaveDTO(prodPackageServiceValueSaveDTOList);
        }

        prodPackageSaveDTO.setProdPackageRelSaveDTO(prodPackageRelSaveDTOList);
        prodPackageSaveDTO.setProdPackageServiceSaveDTO(prodPackageServiceSaveDTOList);

        String str = "";
        try {
            str = prodPackageService.saveProdPackage(prodPackageSaveDTO);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }

        return new AjaxResult(200, str);
    }

    //修改产品套餐信息表
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult updateProdPackage(@RequestBody ProdPackage prodPackage) throws Exception {
        ProdPackageUpdateDTO prodPackageUpdateDTO = BeanUtil.copyProperties(prodPackage, ProdPackageUpdateDTO.class);
        prodPackageUpdateDTO.setMmUserUid(GlobalLoginInfo.getGlobalLoginUser());
        prodPackageUpdateDTO.setMmTime(GlobalLoginInfo.getGlobalLoginTime());

        List prodPackageServiceList = prodPackage.getProdPackageService_();
        List prodPackageServiceUpdateDTOList = Lists.newArrayList();
        for (int i = 0; i < prodPackageServiceList.size(); i++) {
            ProdPackageService_ prodPackageService_ = (ProdPackageService_) prodPackageServiceList.get(i);

            ProdPackageServiceUpdateDTO prodPackageServiceUpdateDTO = new ProdPackageServiceUpdateDTO();
            prodPackageServiceUpdateDTO.setObjuid(prodPackageService_.getObjuid());
            prodPackageServiceUpdateDTO.setProdServiceObjuid(prodPackageService_.getProdServiceObjuid());

            List prodPackageServiceValueList = prodPackageService_.getProdPackageServiceValue();
            List prodPackageServiceValueUpdateDTOList = Lists.newArrayList();
            for (int j = 0; j < prodPackageServiceValueList.size(); j++) {
                ProdPackageServiceValue prodPackageServiceValue = (ProdPackageServiceValue) prodPackageServiceValueList.get(j);

                ProdPackageServiceValueUpdateDTO prodPackageServiceValueUpdateDTO = new ProdPackageServiceValueUpdateDTO();
                prodPackageServiceValueUpdateDTO.setObjuid(prodPackageServiceValue.getObjuid());
                prodPackageServiceValueUpdateDTO.setProdServiceValueUid(prodPackageServiceValue.getProdServiceValueObjuid());
                prodPackageServiceValueUpdateDTOList.add(prodPackageServiceValueUpdateDTO);
            }

            prodPackageServiceUpdateDTO.setProdPackageServiceValueUpdateDTO(prodPackageServiceValueUpdateDTOList);
            prodPackageServiceUpdateDTOList.add(prodPackageServiceUpdateDTO);
        }

        prodPackageUpdateDTO.setProdPackageServiceSaveDTO(prodPackageServiceUpdateDTOList);

        String str = "";
        try {
            str = prodPackageService.updateProdPackage(prodPackageUpdateDTO);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }

        return new AjaxResult(200, str);
    }

    //提交产品套餐信息表
    @RequestMapping(value = "/submitState", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult submitProdPackage(@RequestBody List<ProdPackage> prodPackageList) throws Exception {
        List prodPackageUpdateDTOList = new ArrayList();
        ProdPackageUpdateDTO prodPackageUpdateDTO = null;

        for (int i = 0; i < prodPackageList.size(); i++) {
            ProdPackage prodPackage = prodPackageList.get(i);

            prodPackageUpdateDTO = new ProdPackageUpdateDTO();
            prodPackageUpdateDTO.setObjuid(prodPackage.getObjuid());
            prodPackageUpdateDTOList.add(prodPackageUpdateDTO);
        }

        String str = "";
        try {
            str = prodPackageService.submitProdPackage(prodPackageUpdateDTOList);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }
        return new AjaxResult(200, str);
    }

    //审批通过产品套餐信息表
    @RequestMapping(value = "/passState", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult passProdPackage(@RequestBody List<ProdPackage> prodPackageList) throws Exception {
        List prodPackageUpdateDTOList = new ArrayList();
        ProdPackageUpdateDTO prodPackageUpdateDTO = null;

        for (int i = 0; i < prodPackageList.size(); i++) {
            ProdPackage prodPackage = prodPackageList.get(i);

            prodPackageUpdateDTO = new ProdPackageUpdateDTO();
            prodPackageUpdateDTO.setObjuid(prodPackage.getObjuid());
            prodPackageUpdateDTOList.add(prodPackageUpdateDTO);
        }

        String str = "";
        try {
            str = prodPackageService.passProdPackage(prodPackageUpdateDTOList);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }
        return new AjaxResult(200, str);
    }

    //审批驳回产品套餐信息表
    @RequestMapping(value = "/rejectState", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult rejectProdPackage(@RequestBody ProdPackage prodPackage) throws Exception {
        String str = "";
        try {
            str = prodPackageService.rejectProdPackage(prodPackage);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }
        return new AjaxResult(200, str);
    }

    //设置产品套餐信息
    @RequestMapping(value = "/setTime", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult setTimeProdPackage(@RequestBody ProdPackage prodPackage) throws Exception {
        ProdPackageUpdateDTO prodPackageUpdateDTO = BeanUtil.copyProperties(prodPackage, ProdPackageUpdateDTO.class);

        String str = "";
        try {
            str = prodPackageService.setTimeProdPackage(prodPackageUpdateDTO);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }

        return new AjaxResult(200, str);
    }

    //升版产品套餐信息
    @RequestMapping(value = "/upVersion", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult upVersionProdPackage(@RequestBody ProdPackage prodPackage) throws Exception {
        ProdPackageUpdateDTO prodPackageUpdateDTO = BeanUtil.copyProperties(prodPackage, ProdPackageUpdateDTO.class);

        String str = "";
        try {
            str = prodPackageService.upVersionProdPackage(prodPackageUpdateDTO);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }

        return new AjaxResult(200, str);
    }

    //删除产品套餐信息表
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult deleteProdPackage(@RequestBody List<ProdPackage> prodPackageList) throws Exception {
        List prodPackageDeleteDTOList = new ArrayList();
        ProdPackageDeleteDTO prodPackageDeleteDTO = null;

        for (int i = 0; i < prodPackageList.size(); i++) {
            ProdPackage prodPackage = prodPackageList.get(i);

            prodPackageDeleteDTO = new ProdPackageDeleteDTO();
            prodPackageDeleteDTO.setObjuid(prodPackage.getObjuid());
            prodPackageDeleteDTOList.add(prodPackageDeleteDTO);
        }

        String str = "";
        try {
            str = prodPackageService.deleteProdPackage(prodPackageDeleteDTOList);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }
        return new AjaxResult(200, str);
    }

    //根据主键查询产品套餐唯一数据
    @RequestMapping(value = "/queryByUid", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryProdPackageByUid(@RequestBody ProdPackageQueryDTO prodPackageQueryDTO) throws Exception {
        Map<String, Object> maps = prodPackageService.queryProdPackageByUid(prodPackageQueryDTO.getObjuid());
        ProdPackageQueryVO prodPackageQueryVO = (ProdPackageQueryVO) maps.get("prodPackageQueryVO");
        List prodPackageServiceQueryVOList = (List) maps.get("ProdPackageServiceQueryVOList");

        String jsons = "";
        jsons += "{";
        jsons += "\"prodPackage\":";
        if (prodPackageQueryVO != null) {
            jsons += "[";
            jsons += new Gson().toJson(prodPackageQueryVO);
            jsons += "]";
        }
        jsons += ",\"prodPackageService\":";
        jsons += "[";
        if (prodPackageServiceQueryVOList.size() > 0) {
            for (int i = 0; i < prodPackageServiceQueryVOList.size(); i++) {
                ProdPackageServiceQueryVO prodPackageServiceQueryVO = (ProdPackageServiceQueryVO) prodPackageServiceQueryVOList.get(i);
                jsons += new Gson().toJson(prodPackageServiceQueryVO);
                if (i < (prodPackageServiceQueryVOList.size() - 1)) {
                    jsons += ",";
                }
            }
        }
        jsons += "]";
        jsons += "}";

        return new AjaxResult(200, "SUCCESS", jsons);
    }

    //根据条件查询产品套餐所有数据
    @RequestMapping(value = "/queryAll", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryProdPackageAll(String typeName, String prodName, String speciesName, String speciesApprovalState, int page, int rows) throws Exception {//@RequestBody ProdPackageQueryDTO prodPackageQueryDTO
        Map<String, Object> maps = prodPackageService.queryProdPackageAll(typeName, prodName, speciesName, speciesApprovalState, page, rows);
        int count = (int) maps.get("count");
        List prodPackageQueryVOList = (List) maps.get("prodPackageQueryVOList");

        return new AjaxResult(200, "SUCCESS", new PageInfo(count, prodPackageQueryVOList));
    }

    //根据条件查询产品套餐设置所有数据
    @RequestMapping(value = "/querySetTimeAll", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryProdPackageSetTimeAll(String typeName, String prodName, String speciesName, int page, int rows) throws Exception {//@RequestBody ProdPackageQueryDTO prodPackageQueryDTO
        Map<String, Object> maps = prodPackageService.queryProdPackageSetTimeAll(typeName, prodName, speciesName, page, rows);
        int count = (int) maps.get("count");
        List prodPackageQueryVOList = (List) maps.get("prodPackageQueryVOList");

        return new AjaxResult(200, "SUCCESS", new PageInfo(count, prodPackageQueryVOList));
    }

    //根据条件查询产品套餐审批所有数据
    @RequestMapping(value = "/queryApproveAll", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryProdPackageApproveAll(String typeName, String prodName, String speciesName, int page, int rows) throws Exception {//@RequestBody ProdPackageQueryDTO prodPackageQueryDTO
        Map<String, Object> maps = prodPackageService.queryProdPackageApproveAll(typeName, prodName, speciesName, page, rows);
        int count = (int) maps.get("count");
        List prodPackageQueryVOList = (List) maps.get("prodPackageQueryVOList");

        return new AjaxResult(200, "SUCCESS", new PageInfo(count, prodPackageQueryVOList));
    }

    //根据条件查询产品套餐名称
    @RequestMapping(value = "/queryProdPackageName", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryProdPackageName(String prodName) throws Exception {
        List prodPackageQueryVOList = prodPackageService.queryProdPackageName(prodName);

        String jsons = "";
        jsons += "[";
        if (prodPackageQueryVOList.size() > 0) {
            for (int i = 0; i < prodPackageQueryVOList.size(); i++) {
                ProdPackageQueryVO prodPackageQueryVO = (ProdPackageQueryVO) prodPackageQueryVOList.get(i);
                jsons += new Gson().toJson(prodPackageQueryVO);
                if (i < (prodPackageQueryVOList.size() - 1)) {
                    jsons += ",";
                }
            }
        }
        jsons += "]";

        return new AjaxResult(200, "SUCCESS", jsons);
    }

    //根据条件查询产品套餐服务项及参数
    @RequestMapping(value = "/queryProdPackageServiceNameValue", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryProdPackageServiceNameValue(String speciesName) throws Exception {
        List prodPackageQueryVOList = prodPackageService.queryProdPackageServiceNameValue(speciesName);

        String jsons = "";
        jsons += "[";
        if (prodPackageQueryVOList.size() > 0) {
            for (int i = 0; i < prodPackageQueryVOList.size(); i++) {
                ProdPackageQueryVO prodPackageQueryVO = (ProdPackageQueryVO) prodPackageQueryVOList.get(i);
                jsons += new Gson().toJson(prodPackageQueryVO);
                if (i < (prodPackageQueryVOList.size() - 1)) {
                    jsons += ",";
                }
            }
        }
        jsons += "]";

        return new AjaxResult(200, "SUCCESS", jsons);
    }

}
